package com.wisemoresystem.seal.action.config.datatrans;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_d_datatransfer_config;
import com.wisemoresystem.seal.model.t_menu;
import com.wisemoresystem.seal.service.DatatransferConfigService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 数据移植开关管理
 * 
 * @author Administrator
 *
 */
@Controller("datatrans")
@Scope("prototype")
@RequestMapping("/ix/config/datatrans")
public class DatatransferConfigAction extends ProjBaseAction {

	@Autowired
	private DatatransferConfigService datatransferconfigservice;

	/**
	 * 数据移植开关列表
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		List<Map<String, Object>> list = datatransferconfigservice
				.selectDatatransferConfig();
		String apply = WebTools.getCurrentUserId();

		ModelAndView mv = new ModelAndView("jsp/config/datatransfer/list");
		mv.addObject("list", list);
		mv.addObject("apply", apply);
		return mv;
	}

	@RequestMapping("add_form")
	public ModelAndView add_form() {

		ModelAndView mv = new ModelAndView("jsp/config/datatransfer/add_form");
		return mv;
	}

	/**
	 * 数据移植开关
	 * 
	 * @param dataTransferCconfig_id
	 *            数据移植开关id
	 * @param flag
	 *            状态
	 * @return
	 */
	@RequestMapping("config")
	public ModelAndView config(String dataTransferCconfig_id, String flag) {

		t_d_datatransfer_config dataTransferCconfig = (t_d_datatransfer_config) oc
				.findObjById(dataTransferCconfig_id,
						t_d_datatransfer_config.class);

		if ("申请开启".equals(flag.trim())) {

			oc.delObjById("323", t_menu.class);
			flag = "申请中";
			dataTransferCconfig.setFlag(flag);
			dataTransferCconfig.setApply_user_id(WebTools.getCurrentUserId());
			super.setTipMsg("申请中", Tip.Type.success);
			logT.logH("申请开启数据移植开关", t_d_datatransfer_config.class,
					dataTransferCconfig.get_id());
		} else if ("通过".equals(flag.trim())) {
			flag = "开启";
			dataTransferCconfig.setFlag(flag);
			dataTransferCconfig.setVerify_user_id(WebTools.getCurrentUserId());
			t_menu menu = oc.findObjById("323", t_menu.class);
			menu.setStatus(0);
			oc.saveObj(menu);
			super.setTipMsg("操作通过", Tip.Type.success);
			logT.logH("通过申请开启数据移植开关", t_d_datatransfer_config.class,
					dataTransferCconfig.get_id());
		} else if ("拒绝".equals(flag.trim())) {
			flag = "申请开启";
			dataTransferCconfig.setFlag(flag);
			dataTransferCconfig.setApply_user_id("");
			dataTransferCconfig.setVerify_user_id("");
			oc.delObjById("323", t_menu.class);
			super.setTipMsg("操作已拒绝", Tip.Type.success);
			logT.logH("拒绝申请开启数据移植开关", t_d_datatransfer_config.class,
					dataTransferCconfig.get_id());
		} else if ("开启".equals(flag.trim())) {
			flag = "申请开启";
			dataTransferCconfig.setFlag(flag);
			dataTransferCconfig.setVerify_user_id(WebTools.getCurrentUserId());
			super.setTipMsg("开启成功", Tip.Type.success);
			logT.logH("开启数据移植开关", t_d_datatransfer_config.class,
					dataTransferCconfig.get_id());
		} else if ("关闭".equals(flag.trim())) {
			flag = "申请开启";
			dataTransferCconfig.setFlag(flag);
			dataTransferCconfig.setApply_user_id("");
			dataTransferCconfig.setVerify_user_id("");
			oc.delObjById("323", t_menu.class);
			super.setTipMsg("关闭成功", Tip.Type.success);
			logT.logH("关闭数据移植开关", t_d_datatransfer_config.class,
					dataTransferCconfig.get_id());
		}

		dataTransferCconfig.setUser_id(WebTools.getCurrentUserId());

		oc.updateObj(dataTransferCconfig);

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}
}
